package com.llt.blog.service;

import com.llt.blog.mapper.BlogMapper;
import com.llt.blog.mapper.UserMapper;
import com.llt.blog.model.BlogInfo;
import com.llt.blog.model.UserInfo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;

@Slf4j
@Service
public class UserService {

    @Resource
    private UserMapper userMapper;

    @Resource
    private BlogMapper blogMapper;

    public UserInfo queryUserByName(String userName) {
        return userMapper.selectByName(userName);
    }

    public UserInfo queryUserById(Integer userId) {
        return userMapper.selectById(userId);
    }

    public UserInfo getAuthorInfoByBlogId(Integer blogId) {
        // 1. 根据 博客id 获取 作者id
        BlogInfo blogInfo = blogMapper.selectById(blogId);
        if (blogInfo == null || blogInfo.getUserId() < 1) {
            log.error("该数据不存在,将要返回null");
            return null;
        }
        // 2. 根据 作者id 获取 作者信息
        UserInfo userInfo = userMapper.selectById(blogInfo.getUserId());
        return userInfo;
    }
}
